/**
 * Created by armor on 13-11-7.
 */

Happy = window.Happy || {};


Happy = {
    ENV:{},
    utils:{},
    views:{},
    models:{},
    stores:{}
}


Happy.ENV.levelColor = [
  "#0ac2d2",
  "#7bb7fa",
  "#60d7a9",
  "#fdc162",
  "#fd6a62"
]


Happy.ENV.host = [
    {
        id:"zhaosi",
        name:"赵四",
        desc:"葫芦屯老中医",
        loadingText:"哥先去趟厕所，马上就来，你稍等几秒...",
        welcome:"<h4>那啥，哥是从葫芦屯来的老中医，专治你们这些白领屌丝的不开心。你说你们这点小忧郁算个毛啊，但哥还是要给你诊断诊断，谁让哥仁慈呢，是不？</h4>",
        avtar : "./assets/img/host/avtar-zhaosi.jpg"
    }
]


Happy.JokePage = Backbone.View.extend({
    initialize:function(){
        this.tpl = _.template($("#joke_page").html());
        this.render();
    },

    renderPage:function(model){
        $(".content-wrapper").hide();
        $(".loading .content").html("诶呦？有点意思，哥先google一下药方...");
        $(".loading").show();
        var html = this.tpl({joke:model.toJSON()});
        setTimeout(function(){
            $(".loading").hide();
            $(".content-wrapper").html(html).show();
        },1000);
    },

    render:function(){
        $(".loading").hide();
        var model = new Backbone.Model({
            joke_content : Happy.ENV.host[0].welcome,
            ques: "先说说你开不开心吧"
        });
        $(".content-wrapper").html(this.tpl({joke:model.toJSON()})).show();
    }
})

Happy.App = Backbone.View.extend({
    className:"hg-app",

    initialize:function(){
        this.tpl = _.template($("#app-page").html());
        this.render();
    },

    events:{
        "click .happy":"happy",
        "click .unhappy":"unHappy"
    },

    happy:function(){
        this.$(".content-wrapper").hide();
        this.$(".loading .content").html($("#success-page").html());
        this.$(".loading").show();
    },

    unHappy:function(){
        if(this.jokeStore.length < 1){
            this.$(".content-wrapper").hide();
            this.$(".loading .content").html($("#failure-page").html());
            this.$(".loading").show();
        }else{
            var model = this.jokeStore.shift();
            model.set("ques","这回开心了不？");
            var index = Math.floor(Math.random()*5);
            $("body").css("background-color",Happy.ENV.levelColor[index]);
            this.jokePage.renderPage(model);
        }
    },

    getJokeList:function(){
        var self = this;
        setTimeout(function(){
            $.ajax({
                url:"./api.php?do=jokelist",
                dataType:"json",
                success:function(res){
                    if(res.success){
                        self.jokeStore = new Backbone.Collection(res.data)
                        self.jokePage = new Happy.JokePage();
                    }else{
                        self.$(".content").html("领导跟我说："+res.info+",所以，你最好不要等我了。");
                    }
                },
                error:function(){
                    self.$(".loading .content").html("诶呦妈呀，哥便秘了，一会完不了事，你最好不要等我了，重新挂个号吧");
                }
            });
        },2000);
    },

    render:function(){
        var index = Math.floor(Math.random()*5);
        $("body").css("background-color",Happy.ENV.levelColor[index]);
        this.$el.html(this.tpl({host:{
            avtar : Happy.ENV.host[0].avtar,
            content : Happy.ENV.host[0].loadingText
        }}));
        $(".container").html(this.el);
        this.$(".loading").show();
        this.getJokeList();
    }
})